#coding:utf8
from config import *
from config_mouse_start import *
from routin_simple import *

def deal_add_pressed(st,a):    
    if  a>=1 and a<=9:       
        deal_enter_pressed(st, a,True)
        return
    
    if a==0:
        switch_mouse_tip_l2()
        return
    
    if a==10:        
        switch_mouse_tip_l1()
        return

    if a==13:
        if len(cfg.enter_pressed_seq)>0 and not cfg.isShow:
            deal_13()
        else:
            switch_left_middle_off()   
        return
    
    if a==14:
        switch_show_zi_enable()

    if a==12:
        set_to_last(False)

    if a==15:
        set_to_last(True)

def deal_enter_pressed(st, a,show=False):    
    if a == 0:
        if cfg.w2showing:
            if cfg.x2_mode:
                cfg.x2_mode = False
                clear(True)
            elif is_sca():
                du_exit()         
            else:
                into_x2_mode()   
            return
        cfg.cmd_mode = True
        return
    
    if a==13:     
        if len(cfg.enter_pressed_seq)>0 and not cfg.isShow:
            deal_13()
        else:
            switch_show_jiao()
        return
        

    if a == 11:
        if len(cfg.enter_pressed_seq)>0:
            deal_add()
        else:
            add_showmode()            
        return        
       
    if a == 10:
        switch_show_pos()
        return
    
    if a == 12:
        minu_showmode()        
        return
        
    if 'caps' in st:
        cfg.set_mode  =True
        cfg.set_mode_min = False
        return False
    
    use_pos = False
    if cfg.sum != '0':
        deal_e(False)
        use_pos = True            
    
    if a not in arrm and a!=5:
        return
    if show:
        enter_arr(st,a,show,use_pos,enter_gap2,1)
    else:
        enter_arr(st,a,show,use_pos,enter_gap,enter_ge)

def deal_cmd(st, a):        
    if a >= 0 and a <= 14 and a!=12:
        move_p(a)
        return True
    
    if a==12:
        mouse.Controller().click(mouse.Button.left)         
        return True
    
    if pause_str in st:
        mouse.Controller().click(mouse.Button.left,2)         
        return True
    
    if a == 16:
        cfg.set_mode = True
        cfg.set_mode_min = False
        return False
    
    if 'caps' in st:
        deal_pressed()
        return True

    return False

g_enter_last_posi = True
def enter_dis(a,b):
    global g_enter_last_posi
    ss = '14789632147896321478963214789632'
    a = str(a)
    b = str(b)
    co = 0
    second_idx = 0
    for i in range(len(ss)):
        if ss[i]==a:
            co+=1
            if co>=2:
                second_idx = i
                break
    
    for i in range(4):
        if ss[second_idx+i]==b:
            g_enter_last_posi = True
            return i
        if ss[second_idx-i]==b:
            g_enter_last_posi = False
            return -i
    if g_enter_last_posi:
        return 4
    return -4

def enter_du():
    st = cfg.enter_pressed_seq[0]
    if st==5:
        start_du = cfg.enter5_du
        st = cfg.enter_pressed_seq[1]
    else:
        dx,dy = arrm[st]
        start_du = math.atan2(dy,dx)
    sca = math.pi/4
    for i in cfg.enter_pressed_seq:
        if i==5:
            continue
        if i==st:
            sca /= 4
            continue
        dd = enter_dis(st,i)
        start_du += dd*sca

    return start_du,sca

def enter_find_near(du):
    sm = 1000
    aa = 0
    for a in '12346789':
        a = int(a)
        dx,dy = arrm[a]
        d = math.atan2(dy,dx)
        if abs(d-du)<sm:
            aa = a
            sm = abs(d-du)
    return aa

def enter_arr(st,a,show,use_pos,gap,ge):
    print('enter_arr',ge)
    if a==5:
        if len(cfg.enter_pressed_seq)==0:
            cfg.enter_pressed_seq.append(5)
            cfg.enter5_du,cfg.enter5_len = getdu()
            aa = enter_find_near(cfg.enter5_du)
            cfg.enter_pressed_seq.append(aa)
        else:
            cfg.sum = '6'
            xing()
            return
    else:
        cfg.enter_pressed_seq.append(a)

    
    if len(cfg.enter_pressed_seq)>1:
        cfg.show_jiao = True

    clear_lock()
    is5 = (cfg.enter_pressed_seq[0]==5)
    if is5:
        eg = cfg.enter5_len
    elif show:
        eg = enter_gap2
    else:
        eg = gap
        
    du,sca = enter_du()

    dx = -math.cos(du) * eg
    dy = -math.sin(du) * eg    
    if use_pos:
        p = cfg.fix_pos
    else:
        p = get_cur_pos()

    if len(cfg.enter_pressed_seq)>1:        
        cfg.fix_pos = (p[0] + (dx), p[1] + (dy))
        return
        
    toori = cfg.to
    clear(False, False)    
    cfg.fix_pos = (p[0] + (dx), p[1] + (dy))

    if show:
        cfg.isShow = True
    else:
        to_lock.acquire()
        if len(cfg.enter_pressed_seq)==1:
            cfg.to = eg*ge-10        
        else:
            cfg.to = toori
    
        set_to_chg()
        to_lock.release()
        cfg.isShow =False
        cfg.show_one_on = True
        cfg.show_one_onetime = True        

def deal_add():    
    if cfg.sum=='0.':
        add_kuai()        
        return
    
    if is_ff(cfg.sum) and cfg.w2showing:
        deal_e(True,1)
        return
    if '.' in cfg.sum and not cfg.w2showing and cfg.sum[-1]!='.':
        movexy()
        return 
    
    if cfg.min_ge!=0 and cfg.w2showing and not cfg.min_ge_force_show and cfg.sum=='0':
        cfg.min_ge_force_show = True
        return    
    
    if cfg.sum=='0' and not cfg.w2showing and not is_no_move():
        dotdot()
        return
    
    if cfg.sum=='0' and cfg.w2showing and not cfg.isShow:
        cfg.sum='1'
        xing()
        return
    
    if cfg.sum!='0' and not cfg.w2showing:
        cfg.show_one_on = True
        cfg.show_one_onetime = True        
        xing()
        return

    show_clear()
    if cfg.sum == '0':
        switch_show()
        return    
    deal_e(True,1)

def on_press_inner1(st,a):       
    if a >= 0 and a <= 10:            
        if a == 0 and cfg.sum == '0':
            if is_sca():
                if get_cur_pos() == cfg.sca_cur_pos:
                    du_exit()
                    return 
                update_du(False)
                return 
            clear(True)
            cfg.show_one_on = True
            cfg.last_m_time = time.time()
            return 
        
        cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        
    if a == 14:
        deal_chu()
            
    if a == 12: # -
        deal_minu()
        return
            
    if a == 13: #*
        deal_13()

if __name__ =='__main__':
    d = enter_dis(2,8)
    print(d)